package com.efast.dmom.mes.datahandlers.entity;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.efast.dmom.mes.bean.Label;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.dao.entity.LabelDao;
import com.efast.dmom.mes.datahandlers.base.ConfigurationDataHandler;
import com.efast.dmom.mes.entity.LabelEntity;
import com.efast.dmom.mes.util.SpringWebUtils;
import com.efast.dmom.mes.util.TimeUtil;

@Component
public class LabelDataHandler extends ConfigurationDataHandler<Label>{

	@Autowired
	private LabelDao labelDao;

	@Override
	public ResultBean save(Label obj) {
		ResultBean resultBean = new ResultBean(true,' ');
		LabelEntity labelEntity = obj.getInnerData();
		if (obj.getIsGlobal() == 1 && labelEntity.getPlantId() == 0) {
			// indicates whether the factory is global
			labelEntity.setPlantId(0);
		}
		labelEntity.setSiteId(DataHandlerUtils.getSiteId());
		labelEntity.setCreationTime(TimeUtil.getLocalDateTime());
		labelEntity.setCreationTimeZone(TimeUtil.getLocalTimeZone());
		labelEntity.setCreator(SpringWebUtils.getRequestUser());
		labelEntity.setLastModifiedTime(TimeUtil.getLocalDateTime());
		labelEntity.setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
		labelEntity.setLastModifier(SpringWebUtils.getRequestUser());
//	    labelEntity.setCreationPid(DataHandlerUtils.getDmomPid());
//	    labelEntity.setLastModifiedPid(DataHandlerUtils.getDmomPid());
		labelEntity.setRowStatus(1);
		labelEntity.setRowTrxId(1);
		labelDao.save(labelEntity);
		resultBean.setSuccess(true);
		resultBean.setMessage("保存成功！");
		return resultBean;
	}

	@Override
	public ResultBean update(Label obj) {
		ResultBean resultBean = new ResultBean(true,' ');
		LabelEntity labelEntity = obj.getInnerData();
		if (obj.getIsGlobal() == 1) {
			labelEntity.setPlantId(0);
		}
		labelEntity.setLastModifiedTime(TimeUtil.getLocalDateTime());
		labelEntity.setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
		labelEntity.setLastModifier(SpringWebUtils.getRequestUser());
//	    labelEntity.setLastModifiedPid(DataHandlerUtils.getDmomPid());
		labelEntity.setRowTrxId(1);
		labelDao.update(labelEntity);
		resultBean.setSuccess(true);
		resultBean.setMessage("修改成功！");
		return resultBean;
	}

	@Override
	public ResultBean delete(Label obj) {
		ResultBean resultBean = new ResultBean(true,' ');
		LabelEntity labelEntity = obj.getInnerData();
		labelEntity.setLastModifiedTime(TimeUtil.getLocalDateTime());
		labelEntity.setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
		labelEntity.setLastModifier(SpringWebUtils.getRequestUser());
//	    labelEntity.setLastModifiedPid(DataHandlerUtils.getDmomPid());
		labelEntity.setRowTrxId(1);		
		labelDao.delete(labelEntity);
		resultBean.setSuccess(true);
		resultBean.setMessage("删除成功！");
		return resultBean;
		
	}
	
	public Label getByCode(String labelCode) {
		Label label = new Label();
		LabelEntity labelEntity = labelDao.getByCode(labelCode);
		if (labelEntity != null) {
		label.setInnerData(labelEntity);
		}
		return label;
	}

	@Override
	public Label getById(long id) {
		Label label = new Label();
		LabelEntity labelEntity = labelDao.getById(id);
		if (labelEntity != null) {
		label.setInnerData(labelEntity);
		}
		return label;
	}

	@Override
	public ResultBean saveBatch(List<Label> obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultBean updateBatch(List<Label> obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultBean deleteBatch(List<Label> obj) {
		// TODO Auto-generated method stub
		return null;
	}

}
